package com.helen.ui.account;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.coorchice.library.SuperTextView;
import com.helen.db.UserHelper;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.JsonUtil;
import com.helen.utils.MyLog;
import com.helen.utils.SharedPreferencesUtils;
import com.helen.utils.tiputils.MToast;
import com.helen.widget.MyEditText;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yang on 2018/5/22.
 * 提现
 */

public class TakeCashActivity extends BaseActivity {
    @BindView(R.id.tv_current_balance)
    TextView tvCurrentBalance;
    @BindView(R.id.et_account_name)
    MyEditText etAccountName;
    @BindView(R.id.et_account)
    MyEditText etAccount;
    @BindView(R.id.et_amount)
    MyEditText etAmount;
    @BindView(R.id.cb_zhifubao)
    CheckBox cbZhifubao;
    @BindView(R.id.cb_weixin)
    CheckBox cbWeixin;
    @BindView(R.id.rel_weixin)
    RelativeLayout relWeixin;
    @BindView(R.id.tv_take_cash)
    SuperTextView tvTakeCash;
    private String method = "1";  //1、支付宝 2、微信

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_take_cash;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.take_cash);
        relWeixin.setVisibility(View.GONE);
        String name = (String) SharedPreferencesUtils.getParam(TakeCashActivity.this, Constants.SHARED_PREFERENCES_KEY_AUTH_KEY_NAME, "");
        String account = (String) SharedPreferencesUtils.getParam(TakeCashActivity.this, Constants.SHARED_PREFERENCES_KEY_AUTH_KEY_ACCOUNT, "");
        if (!TextUtils.isEmpty(name)) {
            etAccountName.setText(name);
        }

        if (!TextUtils.isEmpty(account)) {
            etAccount.setText(account);
        }

        RefreshMoneyUtils refreshMoneyUtils = new RefreshMoneyUtils();
        refreshMoneyUtils.refreshMoneyApi(TakeCashActivity.this);
        refreshMoneyUtils.setOnResultListener(new RefreshMoneyUtils.OnResultListener() {
            @Override
            public void onResult(double money, int jinbao) {
                tvCurrentBalance.setText(money + "");
            }
        });
    }

    @Override
    protected void updateViews() {

    }

    @OnClick({R.id.cb_zhifubao, R.id.cb_weixin, R.id.tv_take_cash})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cb_zhifubao:
                cbZhifubao.setChecked(true);
                cbWeixin.setChecked(false);
                break;
            case R.id.cb_weixin:
                method = "2";
                cbWeixin.setChecked(true);
                cbZhifubao.setChecked(false);
                break;
            case R.id.tv_take_cash:
                String name = etAccountName.getText().toString().trim();
                String account = etAccount.getText().toString().trim();
                String money = etAmount.getText().toString().trim();
                String balanceMoney = tvCurrentBalance.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    etAccountName.requestFocus();
                    MToast.makeTextLong(TakeCashActivity.this, "请输入账号姓名").show();
                    return;
                }
                if (TextUtils.isEmpty(account)) {
                    etAccount.requestFocus();
                    MToast.makeTextLong(TakeCashActivity.this, "请输入提现账号").show();
                    return;
                }


                SharedPreferencesUtils.setParam(TakeCashActivity.this, Constants.SHARED_PREFERENCES_KEY_AUTH_KEY_NAME, name);
                SharedPreferencesUtils.setParam(TakeCashActivity.this, Constants.SHARED_PREFERENCES_KEY_AUTH_KEY_ACCOUNT, account);
                if (TextUtils.isEmpty(money)) {
                    etAmount.requestFocus();
                    MToast.makeTextLong(TakeCashActivity.this, "请输入提现金额").show();
                    return;
                }

                taskCashApi(name, account, money);
                break;
        }
    }


    private void taskCashApi(String name, String account, String money) {
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("userid", UserHelper.getInstance().getloginEntity(TakeCashActivity.this).getId() + "");
        params.put("name", name);
        params.put("account", account);
        params.put("money", money);
        params.put("method", method);
        EasyHttp.post(Constants.API_JSONAPI_TAKECASH)
                .params(params)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.d(TAG, "提现失败==" + e.toString());
                        MToast.makeTextShort(TakeCashActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String result) {
                        MyLog.e("yang", "提现成功==" + result);
                        int code = JsonUtil.getJsonValuesInt(result, "code");
                        String msg = JsonUtil.getJsonValuesString(result, "msg");
                        if (code == 0) {
                            MToast.makeTextShort(TakeCashActivity.this, "提现成功,等待审核").show();
                            finish();
                        } else {
                            MToast.makeTextShort(TakeCashActivity.this, msg).show();
                        }
                    }
                });

    }
}
